﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;

namespace Swsk33.SimpleArcEngine.Dialog
{
	public partial class QueryAreaDialog : Form
	{
		/// <summary>
		/// 传入地图控件
		/// </summary>
		private AxMapControl mapControl;

		/// <summary>
		/// 得到的国家图层
		/// </summary>
		private IFeatureLayer countryFeatureLayer;

		public QueryAreaDialog()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 显示查询结果
		/// </summary>
		/// <param name="map">传入地图控件</param>
		public void ShowQueryResult(AxMapControl map)
		{
			mapControl = map;
			// 获取国家图层
			for (int i = 0; i < map.LayerCount; i++)
			{
				countryFeatureLayer = (IFeatureLayer)map.get_Layer(i);
				if (countryFeatureLayer.Name == "country")
				{
					break;
				}
			}
			// 执行查询过程
			// 新建查询器
			IQueryFilter filter = new QueryFilter();
			// 设定查询器的字段查询范围为全部
			filter.SubFields = "*";
			// 设定查询的条件为面积字段大于5000
			filter.WhereClause = "SQKM>5000";
			// 在国家图层中以这个查询器建立游标并执行搜寻
			IFeatureCursor cursor = countryFeatureLayer.Search(filter, false);
			// 获取国家图层属性表中国家名字段的位置
			int fieldIndex = countryFeatureLayer.FeatureClass.FindField("CNTRY_NAME");
			// 将查询器查到的符合条件的要素，获取其国家名并放入列表
			IFeature feature = null;
			while ((feature = cursor.NextFeature()) != null)
			{
				resultList.Items.Add(feature.Value[fieldIndex]);
			}
			ShowDialog();
		}

		/// <summary>
		/// 双击闪烁指定图层
		/// </summary>
		private void resultList_DoubleClick(object sender, System.EventArgs e)
		{
			// 新建查询器
			IQueryFilter filter = new QueryFilter();
			// 设定查询器的字段查询范围为全部
			filter.SubFields = "*";
			// 设定查询的条件为国家名等于选中的列表项的值
			filter.WhereClause = "CNTRY_NAME=\'" + resultList.SelectedItem.ToString() + "\'";
			// 在国家图层中以这个查询器建立游标并执行搜寻
			IFeatureCursor cursor = countryFeatureLayer.Search(filter, false);
			// 闪烁查到的要素
			IFeature feature = null;
			while ((feature = cursor.NextFeature()) != null)
			{
				mapControl.FlashShape(feature.Shape);
			}
		}
	}
}